package util.vehicle;

import java.util.ArrayList;

import exception.BoundaryException;
import signal.Signal;
import util.map.MarsMap;
import util.safe.BlackBox;
import util.show.View;

public class MarsCar extends Vehicle {

	protected MarsMap marsmap;
	private BlackBox blackbox;
	private View view;

	public MarsCar() {
		view = new View();
		marsmap = new MarsMap();
		blackbox = new BlackBox();
		blackbox.addTrace(marsmap);
	}

	public MarsCar(String name, int mass, String color) {
		this();
		this.name = name;
		this.mass = mass;
		this.color = color;

	}

	public void move(Signal s) {
		if (marsmap.isMove(s)) {
			/*
			 * System.out.println(marsmap.getDirDes());
			 * View.displayByDes(marsmap);
			 */
			blackbox.addTrace(marsmap);
			handler();
		} else {
			// System.out.println("不可以离开火星！！！");
			throw new BoundaryException("不可以离开火星！！！");
		}
	}

	public void showTrace() {
		ArrayList<MarsMap> trace = blackbox.getTrace();

		System.out.println("===============");

		for (MarsMap marsmap : trace) {
			handler(marsmap);
		}
	}

	public void showInit() {
		// View.displayByGraphic(marsmap);
		handler();
	}

	public void printPosition() {
		System.out.println(this + "在" + marsmap);
	}

	public String toString() {
		return "" + mass + "吨重的" + color + name + "火星车";
	}

	private void handler() {

		if ("graphic".equals(view.getViewstyle())) {
			displayByGraphic(marsmap);
		}

		if ("desc".equals(view.getViewstyle())) {
			displayByDes(marsmap);
		}
	}

	private void handler(MarsMap marsmap) {

		if ("graphic".equals(view.getViewstyle())) {
			displayByGraphic(marsmap);
		}

		if ("desc".equals(view.getViewstyle())) {
			displayByDes(marsmap);
		}
	}

	private void displayByGraphic(MarsMap marsmap) {

		View.displayByGraphic(marsmap);
	}

	private void displayByDes(MarsMap marsmap) {
		View.displayByDes(marsmap);
	}

	public void setShowStyle(View view) {
		this.view = view;
	}

}